﻿using System;
using System.Collections.Generic;
using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ROMAP.Utilities;

namespace ROMAP.Screens
{
    /// <summary>
    /// This class handles UI elements and entities in the game that do not need to be aware
    /// of other entities.
    /// </summary>
    public class GameScreen : Screen
    {
        #region Fields
        private Sprite _background;
        private Text _timerText;
        private Text _deathText;
        private Level _level;
        private int _deathCounter = 0;

        private double startTime;

        #endregion

        #region Methods

        #region Constructor and Initialize

        public GameScreen()
            : base("GameScreen")
        {
            // Don't put initialization code here, do it in
            // the Initialize method below
            //   |   |   |   |   |   |   |   |   |   |   |
            //   |   |   |   |   |   |   |   |   |   |   |
            //   V   V   V   V   V   V   V   V   V   V   V

        }

        public override void Initialize(bool addToManagers)
        {
            // Set the screen up here instead of in the Constructor to avoid
            // exceptions occurring during the constructor.

            FlatRedBallServices.IsWindowsCursorVisible = false;

            startTime = TimeManager.CurrentTime;

            _timerText = TextManager.AddText("");
            _timerText.Scale = 1.0f;
            _timerText.Spacing = 2.0f;
            _timerText.Visible = false;
            _timerText.AttachTo(SpriteManager.Camera, true);
            _timerText.SetColor(1.0f, 1.0f, 1.0f);
            _timerText.RelativeX = -_timerText.ScaleX - 25;
            _timerText.RelativeY = _timerText.ScaleY + 14;

            _deathText = TextManager.AddText("");
            _deathText.Scale = 1.0f;
            _deathText.Spacing = 1.5f;
            _deathText.Visible = false;
            _deathText.AttachTo(SpriteManager.Camera, true);
            _deathText.SetColor(1.0f, 1.0f, 1.0f);
            _deathText.RelativeX = -_deathText.ScaleX + 15;
            _deathText.RelativeY = _deathText.ScaleY + 14;

            _background = SpriteManager.AddSprite(@"Content/Backgrounds/SpaceSky");
            _background.PixelScale();
            _background.RelativeZ = -1;
            _background.AttachTo(SpriteManager.Camera, true);

            _level = new Level(GameManager.LevelNum);
        }
        #endregion

        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            base.Activity(firstTimeCalled);

            if (!firstTimeCalled)
            {
                //TimeManager.CurrentTime
                ShowTime((TimeManager.CurrentTime - startTime));
                DisplayDeathCount();
                _level.Update();
                if (_level.End)
                {
                    MoveToScreen(typeof(MenuScreen).FullName);
                }
                if (_level.Next)
                {
                    GameManager.DeathCounter += _level.DeathCounter;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
                if (_level.Score)
                {
                    GameManager.DeathCounter += _level.DeathCounter;
                    MoveToScreen(typeof(HiScoreScreen).FullName);
                }
            }
        }

        //Reset the timer on a win
        public void resetTime()
        {
            startTime = TimeManager.CurrentTime;
        }

        public override void Destroy()
        {
            SpriteManager.RemoveSprite(_background);
            _level.Destroy();
            TextManager.RemoveText(_timerText);
            TextManager.RemoveText(_deathText);
            base.Destroy();
        }

        #endregion

        #region Helper Methods

        

        private void ShowTime(double time)
        {
            int minutes = (int)time / 60;

            int seconds = (int)time % 60 ;

            decimal milliseconds = (int)((time - (int)time) * 100);

            string finalTime = minutes + ":" + seconds + "." + milliseconds;

            _timerText.DisplayText = finalTime;
            _timerText.Visible = true;
        }

        private void DisplayDeathCount()
        {
            int add = GameManager.DeathCounter + _level.DeathCounter;

            _deathText.DisplayText = "Deaths: " + add.ToString();
            _deathText.Visible = true;
        }
        #endregion

        #endregion
    }
}

